//
//  MFLCViewController.m
//  Rong360
//
//  Created by tianliwei on 26/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "MFLCViewController.h"
#import "MFLCCell.h"
#import "WebViewController.h"
#import "SubCateViewController.h"
#import "RealEstateQaViewController.h"
#import "EvaluateCalculatorViewController.h"
#import "KFXFViewController.h"
@interface MFLCViewController ()<UITableViewDataSource,UITableViewDelegate,UIFolderTableViewDelegate>

@property (nonatomic, strong)NSArray *mainItems;
@property (nonatomic, strong)NSArray *timeItems;
@property (nonatomic, strong)NSArray *subItems;
@property (nonatomic, strong)SubCateViewController *subVc;
@property (nonatomic, strong)NSIndexPath *selectIndex;
@property (nonatomic, assign)BOOL isOpen;

@end

static NSString *CellIdentifier = @"Cell";


@implementation MFLCViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)buttonClicked:(id)sender{
    UIButton *button = (UIButton *)sender;
    self.hidesBottomBarWhenPushed = YES;
    switch (button.tag) {
        case 0:{
            RealEstateQaViewController *realVC = [[RealEstateQaViewController alloc]init];
            [self.navigationController pushViewController:realVC animated:YES];
            break;
        }
        case 1:{
            EvaluateCalculatorViewController *evalueVC = [[EvaluateCalculatorViewController alloc]init];
            [self.navigationController pushViewController:evalueVC animated:YES];
            break;
        }
        case 2:{
            KFXFViewController *kfxfVC = [[KFXFViewController alloc]init];
            [self.navigationController pushViewController:kfxfVC animated:YES];
            break;
        }
        case 4:{
            RealEstateQaViewController *realVC = [[RealEstateQaViewController alloc]init];
            [self.navigationController pushViewController:realVC animated:YES];
            break;
        }
        case 6:{
            WebViewController *webVC = [[WebViewController alloc]initWithUrl:GJJDKUrl title:@"公积金贷款" desc:GJJDKDesc];
            webVC.type = HTML5ShareUrl;
            [self.navigationController pushViewController:webVC animated:YES];
            break;
        }
        case 9:{
            WebViewController *webVC = [[WebViewController alloc]initWithUrl:MFSFUrl title:@"买房税费" desc:MFSFDesc];
            webVC.type = HTML5ShareUrl;
            [self.navigationController pushViewController:webVC animated:YES];
            break;
        }
        default:
            break;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"买房流程";
    self.timeItems = @[@"",@"",@"",@"0.5个",@"3~10个",@"0.5个",@"15个",@"3~7个",@"0.5个",@"1个",@"5~10个",@"N个",@""];
    self.view.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_NAVIFRAME_HEIGHT);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"MFLCCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    self.tableView.rowHeight = 55;
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"mflc" ofType:@"plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    self.mainItems = [dic objectForKey:@"main"];
    self.subItems = [dic objectForKey:@"sub"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mainItems.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MFLCCell *cell = (MFLCCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (indexPath.row == 0 ||indexPath.row == 1 || indexPath.row == 2) {
        cell.dateLabel.hidden = YES;
    }
    cell.timeLabel.text = [self.timeItems objectAtIndex:indexPath.row];
    cell.titleLabel.text = [self.mainItems objectAtIndex:indexPath.row];
    [cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
    return cell;
}


#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MFLCCell *cell = (MFLCCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    [cell changeArrowWithUp:YES];
    self.subVc = [[SubCateViewController alloc]
                                    initWithNibName:NSStringFromClass([SubCateViewController class])
                                    bundle:nil];
    self.subVc.vc = self;
    self.subVc.tag = indexPath.row;
    self.subVc.info = [self.subItems objectAtIndex:indexPath.row];
    self.tableView.scrollEnabled = NO;
    UIFolderTableView *folderTableView = (UIFolderTableView *)tableView;
    [folderTableView openFolderAtIndexPath:indexPath WithContentView:self.subVc.view
                                 openBlock:^(UIView *subClassView, CFTimeInterval duration, CAMediaTimingFunction *timingFunction){
                                     // opening actions
                                     [self CloseAndOpenACtion:indexPath];
                                 }
                                closeBlock:^(UIView *subClassView, CFTimeInterval duration, CAMediaTimingFunction *timingFunction){
                                    // closing actions
                                    self.tableView.scrollEnabled = YES;
                                    [cell changeArrowWithUp:NO];
                                }
                           completionBlock:^{
                               // completed actions
                           }];
}


-(void)CloseAndOpenACtion:(NSIndexPath *)indexPath
{
    if ([indexPath isEqual:self.selectIndex]) {
        self.isOpen = NO;
        [self didSelectCellRowFirstDo:NO nextDo:NO];
        self.selectIndex = nil;
    }
    else
    {
        if (!self.selectIndex) {
            self.selectIndex = indexPath;
            [self didSelectCellRowFirstDo:YES nextDo:NO];
            
        }
        else
        {
            [self didSelectCellRowFirstDo:NO nextDo:YES];
        }
    }
}


- (void)didSelectCellRowFirstDo:(BOOL)firstDoInsert nextDo:(BOOL)nextDoInsert
{
    self.isOpen = firstDoInsert;
    
    MFLCCell *cell = (MFLCCell *)[self.tableView cellForRowAtIndexPath:self.selectIndex];
    [cell changeArrowWithUp:firstDoInsert];
    
    if (nextDoInsert) {
        self.isOpen = YES;
        self.selectIndex = [self.tableView indexPathForSelectedRow];
        [self didSelectCellRowFirstDo:YES nextDo:NO];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
